<?php
class ui_callback_default extends ui {
	public function __construct($S, $param) {
		parent::__construct();
		if ($_GET['code'] and $_GET['state']) {
			$state = data_social::check_state($_GET['state']);		// array ('social_type', 'flag')
			if ($state) {
				$social_name = array (
					'weibo'	=> '新浪微博',
					'renren'	=> '人人网',
					'kaixin001'	=> '开心网',
					'qqwb'	=> '腾讯微博',
				);
				$token = data_social::code_to_token($state['social_type'], $_GET['code']);		//  (uid, access_token, refresh_token, ctime, expires_in, name, nick)
				if ($state['flag'] == 'setting' and $_SESSION['user_id']) {
					if ($token) {
						// 登陆成功，合并用户
						$data_user = new data_user();
						if ($data_user->merge_user($_SESSION['user_id'], $state['social_type'], $token)) {		// merge user, add user and other account, Event update friends by accout type
							//  goto setting
							$S->assign('html_header', loadMod('glob/header'), true);
							$S->assign('msg', '<div class="mblue">绑定账号成功！</div>', true);
							$S->assign('url', '/friend.php', true);
							return false;
						} else {
							// show failed , wait and goto setting
							$S->assign('html_header', loadMod('glob/header'), true);
							$S->assign('msg', '<div class="myell">合并账号时发生错误！请稍后重试！</div>', true);
							$S->assign('url', '/friend.php', true);
							return false;
						}
					} else {
						// show authorize failed , wait and goto setting
						$S->assign('html_header', loadMod('glob/header'), true);
						$S->assign('msg', '<div class="myell">'.$social_name[$state['social_type']].'登陆授权发生错误！</div>', true);
						$S->assign('url', '/friend.php', true);
						return false;
					}
				} else {
					$data_login = new data_login();
					$uid = $data_login->login_by_token($state['social_type'], $token);		// 如果不是注册用户，那么就把他注册成为用户
					if ($uid) {	
						// 登陆成功
						if ($state['flag'] == 'iframe') {
							if ($state['social_type'] == 'qqwb' or $state['social_type'] == 'kaixin001') {
								// 如果是腾讯的话，腾讯会不在iframe里返回，而是直接在父窗口返回。所以得用history.back()来返回登录前的那页
								// 现在kaixin001也是这样了，因为腾讯收购它了吗？
								$S->assign('html_header', loadMod('glob/header'), true);
								$S->assign('msg', '<div class="mblue">'.$social_name[$state['social_type']].'登陆成功！</div>', true);
								if ($state['social_type'] == 'kaixin001') {
									$S->assign('url', 'history_back2', true);
								} else {
									$S->assign('url', 'history_back', true);
								}

								return false;
							} else {
								// show success , stop
								$S->assign('html_header', loadMod('glob/header'), true);
								$S->assign('msg', '<div class="mblue">'.$social_name[$state['social_type']].'登陆成功！</div>', true);
								$S->assign('url', 'stop', true);
								return false;
							}
						} else {
							// go to index
							header('Location:/');
							die();
						}
					} else {
						if ($state['flag'] == 'iframe') {
							// show failed , wait and to login
							$S->assign('html_header', loadMod('glob/header'), true);
							$S->assign('msg', '<div class="myell">'.$social_name[$state['social_type']].'登陆失败！</div>', true);
							$S->assign('url', '/login.php', true);
							return false;
						} else {
							// show failed , wait and to index
							$S->assign('html_header', loadMod('glob/header'), true);
							$S->assign('msg', '<div class="myell">'.$social_name[$state['social_type']].'登陆失败！</div>', true);
							$S->assign('url', '/', true);
							return false;
						}
					}
				}
			} else {
				// check state failed , wait and to index
				$S->assign('html_header', loadMod('glob/header'), true);
				$S->assign('msg', '<div class="myell">'.$social_name[$state['social_type']].'登陆失败！</div>', true);
				$S->assign('url', '/', true);
				return false;
			}
		} else {
			header('Location:/');
		}
	}
}
?>
